package com.zryc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.service.IBrandInfoService;
import com.zryc.shop.entity.BrandInfo;
import com.zryc.shop.utils.Result;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 品牌信息表 前端控制器
 * </p>
 *
 * @author xujiajun
 * @since 2022-10-17
 */
@RestController
@RequestMapping("/brand")
@Api(tags = {"品牌管理的接口列表"})
public class BrandInfoController {
    @Autowired
    private IBrandInfoService brandInfoService;

    @PostMapping(path = "/create",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("管理员_添加商品")
    public Result<BrandInfo> add(@RequestBody BrandInfo brandInfo){
        try{
            brandInfoService.save(brandInfo);
            return new Result<>(true,200,"品牌添加完成",brandInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new Result<>(false,500,"添加品牌出错:"+e.getMessage());
        }
    }

    @GetMapping(path = "/list")
    @ApiOperation("查询品牌列表")
    public Result list(@ApiParam("品牌名称") @RequestParam(required = false,name = "keyword") String brandName,
                                @ApiParam(value = "分页页号",example = "1") @RequestParam(defaultValue = "1") Integer pageNum,
                                @ApiParam(value = "分页大小",example = "5") @RequestParam(defaultValue = "5") Integer pageSize
                                ){
        QueryWrapper wrapper=new QueryWrapper();
        if (StringUtils.isNotEmpty(brandName)){
            wrapper.like("brand_name",brandName);
        }
        IPage page=brandInfoService.page(new Page<BrandInfo>(pageNum,pageSize),wrapper);
        return new Result(true,200,"查询完成",page);

    }

    //查询品牌详情
    @GetMapping("/{brandId}")
    @ApiOperation("查询品牌详情")
    public Result<BrandInfo> detail(@ApiParam(value = "品牌ID",example = "3",required = true) @PathVariable("brandId") Integer brandId){
        BrandInfo brandInfo=brandInfoService.getById(brandId);
        if (brandInfo!=null){
            return new Result<>(true,200,"查询成功",brandInfo);
        }else{
            return new Result<>(false,404,"未查询到结果,可能品牌ID不存在");
        }
    }

    @PostMapping(path = "/update/{brandId}",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("更新品牌信息")
    public Result<BrandInfo> update(@RequestBody BrandInfo brandInfo,@PathVariable Integer brandId){
        try {
            brandInfo.setBrandId(brandId.shortValue());
            boolean result=brandInfoService.updateById(brandInfo);
            if (result){
                return new Result<>(true,200,"修改成功");
            }else{
                return new Result<>(false,304,"修改失败");
            }
        }catch (Exception e){
            return new Result<>(false,500,"执行出现错误");
        }

    }

    @RequestMapping(path = "/delete/{brandId}",method = RequestMethod.GET)
    @ApiOperation("删除品牌信息")
    public Result remove(@ApiParam(value = "品牌ID" ,required = true) @PathVariable("brandId") Integer brandId){
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("brand_id",brandId);
        wrapper.set("brand_status",0);
        if (brandInfoService.update(wrapper)){
            return new Result(true,200,"删除成功");
        }else{
            return new Result(false,304,"删除失败");
        }
    }

    @PostMapping(path = "/recover/{brandId}")
    @ApiOperation("恢复删除品牌信息")
    public String recover(@ApiParam(value = "品牌ID" ,required = true) @PathVariable("brandId") Integer brandId){
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("brand_id",brandId);
        wrapper.set("brand_status",1);
        if (brandInfoService.update(wrapper)){
            return "OK";
        }else{
            return "FAIL";
        }
    }




}
